hysop.numerics.stencil.stencil_generator module

Finite differences stencil inteface used in various numerical methods.

class hysop.numerics.stencil.stencil_generator.CenteredStencilGenerator(config=None, cache_override=False)[source]

Bases: StencilGenerator

Generate various centered stencils. Results are cached and compressed locally.

StencilGenerator to generate stencils of dimension dim and type dtype. Results are cached and compressed locally.

dim

Dimension of the generated stencils.

Type:

int

dtype

Datatype of the generated stencils.

Type:

{np.float32, np.float64, MPQ}

Parameters:
  • dim (int) – Dimension of the generated stencils.

  • dtype ({np.float32, np.float64, MPQ}) – Datatype of the generated stencils.

  • cache_override (bool) – If set, overwrite already cached stencils.

Raises:
  • ValueError – Raised if dim<1 or if dtype in [np.float32, np.float64] and dim!=1.

  • TypeError – Raised if dtype is not a valid type.

Notes

Depending on data type dtype a different solver can be used. For fast generation of 1D stencils, use np.float32 or np.float64. For n-dimentional exact fractional coefficients stencils use the default mpq type. Exact fractional results (mpq) are cached locally.

Examples

Generate 1D approximative forward first derivative stencil of order 1 and 2.

>>> import numpy as np
>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator
>>> generator = StencilGenerator().configure(dim=1,dtype=np.float64)
>>> s0 = generator.generate_approximative_stencil(origin=0, derivative=1, order=1)
>>> s1 = generator.generate_approximative_stencil(origin=0, derivative=1, order=2)
>>> print('{}\n{}'.format(s0.coeffs,s1.coeffs))
[-1.  1.]
[-1.5  2.  -0.5]

Generate 1D exact backward first derivative stencil of order 1 and 2.

>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator
>>> generator = StencilGenerator().configure(dim=1)
>>> s0 = generator.generate_exact_stencil(origin=-1, derivative=1, order=1)
>>> s1 = generator.generate_exact_stencil(origin=-1, derivative=1, order=2)
>>> print('{}\n{}'.format(s0.coeffs,s1.coeffs))
[-1 1]
[1/2 -2 3/2]

Generate centered 2D Laplacian of order 2:

>>> import sympy as sm
>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator
>>> generator = StencilGenerator().configure(dim=2, derivative=2)
>>> laplacian = generator.generate_exact_stencil(origin=1, order=2, dx=(sm.Symbol('dx'),)*2)
>>> print(laplacian.coeffs)
[[0 1 0]
 [1 -4 1]
 [0 1 0]]

Similarly, with different custom dx:

>>> laplacian = generator.generate_exact_stencil(origin=1, order=2)
>>> print(laplacian.coeffs)
[[0 dx**(-2) 0]
 [dy**(-2) (-2*dx**2 - 2*dy**2)/(dx**2*dy**2) dy**(-2)]
 [0 dx**(-2) 0]]

See also

Stencil

generate_approximative_stencil(**kargs)[source]

Generate a stencil using hardware floating point arithmetic (fast). dtype can be np.float16, np.float32, np.float64, MPQ

generate_exact_stencil(**kargs)[source]

Generate a stencil using quotients (slower). Yield exact solutions by using symbolic calculation and quotient arithmetic. Do not use on high order stencils.

class hysop.numerics.stencil.stencil_generator.StencilGenerator(config=None, cache_override=False)[source]

Bases: object

Generate various stencils. Results are cached and compressed locally.

StencilGenerator to generate stencils of dimension dim and type dtype. Results are cached and compressed locally.

dim

Dimension of the generated stencils.

Type:

int

dtype

Datatype of the generated stencils.

Type:

{np.float32, np.float64, MPQ}

Parameters:
  • dim (int) – Dimension of the generated stencils.

  • dtype ({np.float32, np.float64, MPQ}) – Datatype of the generated stencils.

  • cache_override (bool) – If set, overwrite already cached stencils.

Raises:
  • ValueError – Raised if dim<1 or if dtype in [np.float32, np.float64] and dim!=1.

  • TypeError – Raised if dtype is not a valid type.

Notes

Depending on data type dtype a different solver can be used. For fast generation of 1D stencils, use np.float32 or np.float64. For n-dimentional exact fractional coefficients stencils use the default mpq type. Exact fractional results (mpq) are cached locally.

Examples

Generate 1D approximative forward first derivative stencil of order 1 and 2.

>>> import numpy as np
>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator
>>> generator = StencilGenerator().configure(dim=1,dtype=np.float64)
>>> s0 = generator.generate_approximative_stencil(origin=0, derivative=1, order=1)
>>> s1 = generator.generate_approximative_stencil(origin=0, derivative=1, order=2)
>>> print('{}\n{}'.format(s0.coeffs,s1.coeffs))
[-1.  1.]
[-1.5  2.  -0.5]

Generate 1D exact backward first derivative stencil of order 1 and 2.

>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator
>>> generator = StencilGenerator().configure(dim=1)
>>> s0 = generator.generate_exact_stencil(origin=-1, derivative=1, order=1)
>>> s1 = generator.generate_exact_stencil(origin=-1, derivative=1, order=2)
>>> print('{}\n{}'.format(s0.coeffs,s1.coeffs))
[-1 1]
[1/2 -2 3/2]

Generate centered 2D Laplacian of order 2:

>>> import sympy as sm
>>> from hysop.numerics.stencil.stencil_generator import StencilGenerator
>>> generator = StencilGenerator().configure(dim=2, derivative=2)
>>> laplacian = generator.generate_exact_stencil(origin=1, order=2, dx=(sm.Symbol('dx'),)*2)
>>> print(laplacian.coeffs)
[[0 1 0]
 [1 -4 1]
 [0 1 0]]

Similarly, with different custom dx:

>>> laplacian = generator.generate_exact_stencil(origin=1, order=2)
>>> print(laplacian.coeffs)
[[0 dx**(-2) 0]
 [dy**(-2) (-2*dx**2 - 2*dy**2)/(dx**2*dy**2) dy**(-2)]
 [0 dx**(-2) 0]]

See also

Stencil

CROSS = 1
CUSTOM = 99
DENSE = 0
DIAG = 2
classmethod cache_file()[source]
cache_override()[source]

Get caching override flag.

configure(config=None, **kargs)[source]

Push a new stencil generator configuration by keyword arguments (see StencilGeneratorConfiguration.configure() or by StencilGeneratorConfiguration instance.

generate_approximative_stencil(origin, **kargs)[source]

Generate a stencil using hardware floating point arithmetic (fast). dtype can be np.float16, np.float32, np.float64, MPQ

generate_exact_stencil(origin, **kargs)[source]

Generate a stencil using quotients (slower). Yield exact solutions by using symbolic calculation and quotient arithmetic. Do not use on high order stencils.

get_config()[source]

Get current stencil generator configuration state.

class hysop.numerics.stencil.stencil_generator.StencilGeneratorConfiguration[source]

Bases: object

L(origin)[source]
R(origin)[source]
configure(dim=None, dtype=None, dx=None, user_eqs=None, derivative=None, order=None, mask=None, mask_type=None)[source]

Configure the stencil generator.

copy()[source]
mask(origin, custom_mask=None)[source]
shape()[source]
symbolic_derivatives(extra_size=0)[source]
symbolic_stencil(origin)[source]